#!/usr/bin/env python
#coding:utf-8
#__author__="ybh"
from aliyunsdkcore.client import AcsClient
from aliyunsdkcdn.request.v20141111 import SetDomainServerCertificateRequest
from web import models
from django.shortcuts import HttpResponse
import time
import json
import hashlib

def update_https(cdnobj,certificate,privatekey):
    domain_name=cdnobj.domain_name
    access_id=cdnobj.account.access_id
    access_secret=cdnobj.account.access_secret
    date_time=time.strftime("%Y%m%d",time.localtime(time.time()))
    certname="%s-%s" % (domain_name,date_time)
    client = AcsClient(access_id, access_secret, 'cn-hangzhou')

    # 设置参数
    request = SetDomainServerCertificateRequest.SetDomainServerCertificateRequest()
    request.set_accept_format('json')

    request.add_query_param('DomainName', domain_name)
    request.add_query_param('ServerCertificateStatus', 'on')
    request.add_query_param('CertName', certname)
    request.add_query_param('ServerCertificate', certificate)
    request.add_query_param('PrivateKey', privatekey)

    # 发起请求
    response=client.do_action_with_exception(request).decode('utf-8')

    return response


